﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLKDS.DAL;
using DevExpress.XtraEditors.DXErrorProvider;
using QLKDS.DTO;

namespace QLKDS.Forms
{
    public partial class frmStoreEdit : XtraForm
    {
        #region Properties
        private StoreManager storeManager;
        public StoreInfo Store { get; set; }
       
        #endregion

        #region Init Form
        public frmStoreEdit()
        {
            InitializeComponent();

            // Initialize all properties of this form
            this.storeManager = new StoreManager();
            this.Store = new StoreInfo();
            
        }

        
        private void frmStoreEdit_Load_1(object sender, EventArgs e)
        {
            this.txtName.Text = this.Store.Name;
            this.txtAddress.Text = this.Store.Address;
            this.txtMobile.Text = this.Store.Mobile;
            this.txtPhone.Text = this.Store.Phone;
            this.txtTaxCode.Text = this.Store.TaxCode;
            this.txtDescription.Text = this.Store.Description;
            this.dteModifiedDate.DateTime = DateTime.Now;
            this.dteModifiedDate.Properties.MaxValue = DateTime.Now;
            this.txtContactor.Text = this.Store.Contactor;
            this.txtContactorEmail.Text = this.Store.ContactorEmail;
            this.txtContactorMobile.Text = this.Store.ContactorMobile;
            this.txtContactorPhone.Text = this.Store.ContactorPhone;
        }
        #endregion

        #region Methods

        private void SetMaxLengthOfTextBoxes()
        {
            this.txtName.Properties.MaxLength = 32;
            this.txtMobile.Properties.MaxLength = 16;
            this.txtPhone.Properties.MaxLength = 16;
        }

        /// <summary>
        /// Clear all Textboxes on this form
        /// </summary>
        private void ClearTextBoxes()
        {
            this.txtName.Text = String.Empty;
            this.txtMobile.Text = String.Empty;
            this.txtPhone.Text = String.Empty;
            this.txtAddress.Text = String.Empty;
            this.txtTaxCode.Text = string.Empty;
            this.txtDescription.Text = String.Empty;
            this.dteModifiedDate.DateTime = DateTime.Now;
            this.dteModifiedDate.Properties.MaxValue = DateTime.Now;
            this.txtContactor.Text = string.Empty;
            this.txtContactorEmail.Text = string.Empty;
            this.txtContactorMobile.Text = string.Empty;
            this.txtContactorPhone.Text = string.Empty;
            this.txtName.Focus();
        }
        #endregion

        #region Validate

        private void txtName_EditValueChanged(object sender, EventArgs e)
        {
            if (this.txtName.Text.Trim() == String.Empty)
            {
                //
            }
            else
            {
                this.dxepName.ClearErrors();
            }
        }
        private void txtName_Leave(object sender, EventArgs e)
        {
            if (this.txtName.Text.Trim() == String.Empty)
            {
                this.dxepName.SetError(this.txtName, "Tên khách hàng không được để trống.", ErrorType.User1);
            }
        }




        private void txtMobile_EditValueChanged(object sender, EventArgs e)
        {
            if (this.txtMobile.Text.Trim() == String.Empty)
            {
                //
            }
            else
            {
                if (this.storeManager.HasMobileDuplicated(this.txtMobile.Text.Trim(), this.Store.Id))
                {
                    this.dxepMobile.SetError(this.txtMobile, "Số điện thoại " + this.txtMobile.Text.Trim() + " đã tồn tại trong hệ thống. Vui lòng kiểm tra lại.", DevExpress.XtraEditors.DXErrorProvider.ErrorType.User1);
                    this.txtMobile.Focus();
                    return;
                }

                this.dxepMobile.ClearErrors();
            }
        }



        #endregion

        #region button_click

        private void btnClear_Click(object sender, EventArgs e)
        {
            this.ClearTextBoxes();
        }


        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            //id = this.Store.Id;
            var store = new Store();

            // Validation
            if (this.txtName.Text.Trim().Length == 0)
            {
                this.dxepName.SetError(this.txtName, "Tên khách hàng không được để trống.", DevExpress.XtraEditors.DXErrorProvider.ErrorType.User1);
                this.txtName.Focus();
                return;
            }
            else
            {
                this.dxepName.ClearErrors();
            }


            if (this.txtModifiedBy.Text.Trim().Length == 0)
            {
                this.dxepModifiedBy.SetError(this.txtModifiedBy, "Lựa chọn tên nhân viên sửa đổi");
                this.txtModifiedBy.Focus();
                return;
            }
            // End of validation
            /*****************************************************************/
            // Save into database
            store.Id = this.Store.Id;
            store.Name = this.txtName.Text.Trim();
            store.Mobile = this.txtMobile.Text.Trim();
            store.Phone = this.txtPhone.Text.Trim();
            store.Address = this.txtAddress.Text.Trim();
            store.TaxCode = this.txtTaxCode.Text.Trim();
            store.Description = this.txtDescription.Text.Trim();

            store.Status = 1;

            store.CreatedBy = this.Store.CreatedBy;
            store.CreatedDate = DateTime.Now;
            store.ModifiedBy = this.txtModifiedBy.Text;
            store.ModifiedDate = this.dteModifiedDate.DateTime;
            store.Contactor = this.txtContactor.Text.Trim();
            store.ContactorEmail = this.txtContactorEmail.Text.Trim();
            store.ContactorMobile = this.txtMobile.Text.Trim();
            store.ContactorPhone = this.txtPhone.Text.Trim();
            this.storeManager.UpdateStore(store);
            XtraMessageBox.Show("Sửa dữ liệu thành công");
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        #endregion

        

   
    }
}
